package pl.lodz.p.browarex.validator;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import pl.lodz.p.browarex.model.Customer;
import pl.lodz.p.browarex.model.enums.ClientType;

@Component
public class UserRegistrationValidator implements Validator {
	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
			+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	private static final String POST_CODE = "[0-9]{2}(-[0-9]{3})";
	private static final String PHONE_NUMBER = "[0-9]{9}";
	private static final String PESEL = "[0-9]{11}";
	private static final String REGON = "[0-9]{9}";

	public boolean supports(Class<?> type) {
		return type == Customer.class;
	}

	public void validate(Object target, Errors errors) {
		Customer customer = (Customer) target;
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.add(Calendar.YEAR, -18);

		// Sprawdzane nie wazne czy firma czy osoba prywatna
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username",
				"field.empty.error");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
				"field.empty.error");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "postalCode",
				"field.empty.error");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "city",
				"field.empty.error");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email",
				"field.empty.error");
		if (customer.getEmail() != null && !"".equals(customer.getEmail())) {
			if (!checkRegExp(customer.getEmail(), EMAIL_PATTERN)) {
				errors.rejectValue("email", "email.format.error");
			}
		}
		if (customer.getPostalCode() != null
				&& !"".equals(customer.getPostalCode())) {
			if (!checkRegExp(customer.getPostalCode(), POST_CODE)) {
				errors.rejectValue("postalCode", "postalCode.value.error");
			}
		}
		if (customer.getPhoneNumber() != null
				&& !"".equals(customer.getPhoneNumber())) {
			if (!checkRegExp(customer.getPhoneNumber(), PHONE_NUMBER)) {
				errors.rejectValue("phoneNumber", "phonenumber.value.error");
			}
		}
		if (ClientType.PRIVATE_CUSTOMER.toString().equals(
				customer.getClientType().toString())) {
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
					"field.empty.error");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname",
					"field.empty.error");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "birthDate",
					"field.empty.error");
			if (customer.getBirthDate() != null) {
				if (customer.getBirthDate().after(
						new Date(calendar.getTimeInMillis()))) {
					errors.rejectValue("birthDate", "birthDate.value.error");
				}
			}
			if (customer.getPESEL() != null && !"".equals(customer.getPESEL())) {
				if (!checkRegExp(customer.getPESEL(), PESEL)) {
					errors.rejectValue("PESEL", "pesel.value.error");
				}
			}
		}
		if (ClientType.COMPANY.toString().equals(
				customer.getClientType().toString())) {
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "comapanyName",
					"field.empty.error");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "REGON",
					"field.empty.error");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "NIP",
					"field.empty.error");
			if (customer.getREGON() != null
					&& !"".equals(customer.getREGON())) {
				if (!checkRegExp(customer.getREGON(), REGON)) {
					errors.rejectValue("REGON", "regon.value.error");
				}
			}
		}
		System.out.println("Validacja PYK");

	}

	private boolean checkRegExp(String value, String regex) {
		Pattern pattern;
		Matcher matcher;
		pattern = Pattern.compile(regex);
		matcher = pattern.matcher(value);
		return matcher.matches();
	}
}
